package fi.iki.juri.units.generator;

import java.util.Iterator;
import java.util.NoSuchElementException;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * Wraps NodeList in an Iterable.
 * 
 * @author Juri Pakaste <juri@iki.fi>
 */
public class IterableNodeList implements Iterable<Element> {
	private final NodeList nodeList;

	public IterableNodeList(final NodeList nodeList) {
		this.nodeList = nodeList;
	}

	public Iterator<Element> iterator() {
		return new Iterator<Element>() {
			int i = 0;
			
			public boolean hasNext() {
				return i < nodeList.getLength();
			}

			public Element next() {
				if (i >= nodeList.getLength())
					throw new NoSuchElementException("Iterator out of range");
				return (Element) nodeList.item(i++);
			}

			public void remove() {
				throw new UnsupportedOperationException("NodeList iterator is read-only");
			} };
	}
}
